﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElementumForever.Interfaces;

namespace ElementumForever.CommandPattern
{
    public class CommandManager
    {
        List<ICommand> _undoList;
        List<ICommand> _redoList;
        IsceneBuilder _modeler;

        public CommandManager(IsceneBuilder modeler) {
            this._modeler = modeler;
            _undoList = new List<ICommand>();
            _redoList = new List<ICommand>();
        }

        public void NewCommand(ICommand command) {//add mew command
            command.Execute(this._modeler);
            _undoList.Add(command);
        }

        public void Redo() {
            ICommand command = Pop(_redoList);
            command.Execute(this._modeler);
            _undoList.Add(command);
        }

        public void Undo() {
            ICommand command=Pop(_undoList);
            command.UnExecute(this._modeler);
            _redoList.Add(command);
        }

        private ICommand Pop(List<ICommand> list) {
            if (list.Count == 0)
                throw new Exception("empty list!!");
            ICommand command = list[list.Count-1];
            list.RemoveAt(list.Count - 1);
            return command;
        }

        public bool EnableUndo {
            get { return _undoList.Count > 0; }
        }

        public bool EnableRedo
        {
            get { return _redoList.Count > 0; }
        }

        
    }
}
